﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;

namespace LuKou.Web.Core
{
    public static class Text
    {
        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }

        public static bool IsNullOrEmptyOrWhiteSpace(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return true;
            }
            if (string.IsNullOrEmpty(s.Trim()))
            {
                return true;
            }
            return false;
        }

        public static bool IsBooleanString(this string s)
        {
            s = s.Trim().ToLower();
            if (!(s == "true") && !(s == "false"))
            {
                return false;
            }
            return true;
        }

        public static bool IsMobileNo(this string s)
        {
            if (string.IsNullOrEmpty(s)) return false;
            return Regex.IsMatch(s, @"^(13[0-9]|15[14589]|18[89])\d{8}$");
        }

        /// <summary>
        /// 是否是个整数
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsInteger(this string s)
        {
            if (string.IsNullOrEmpty(s)) return false;
            return Regex.IsMatch(s, @"^\d+$");
        }

        public static string RemoveHtml(this string s)
        {
            if (s.IsNullOrEmpty()) return "";
            return Regex.Replace(s, @"<\/?[^>]+>", string.Empty);
        }

        public static string Substr(this string str, int length)
        {
            if (str.IsNullOrEmpty()) return "";
            if (str.Length <= length)return str;
            return str.Substring(0, length) + "...";
        }

        public static string SqlEncode(this string s)
        {
            StringBuilder builder = new StringBuilder(s);
            builder.Replace("'", "''");
            return builder.ToString();
        }

        public static bool ToBoolean(this int i)
        {
            return (i != 0);
        }

        public static bool ToBoolean(this string s)
        {
            bool flag = false;
            bool.TryParse(s, out flag);
            return flag;
        }
        /// <summary>
        /// 字符转数字
        /// </summary>
        /// <param name="s"></param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static int ToInt32(this string s, int defaultvalue)
        {
            int.TryParse(s, out defaultvalue);
            return defaultvalue;
        }

        public static int ToInt32(this string s)
        {
            return s.ToInt32(0);
        }

        /// <summary>
        /// 转换成链接字符串
        /// </summary>
        /// <param name="list"></param>
        /// <param name="joinString">链接字符</param>
        /// <returns></returns>
        public static string ToJoinString(this List<string> list, string joinString)
        {
            string returnRen = "";

            foreach (string item in list)
            {
                if (!string.IsNullOrEmpty(returnRen))
                    returnRen += joinString;

                returnRen += item;
            }

            return returnRen;
        }

        /// <summary>
        /// MD5 加密(32位)
        /// </summary>
        /// <param name="s"></param>
        /// <returns>32位密码字符串</returns>
        public static string ToMD5(this string s)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(s, "MD5").ToLower();
        }

        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="s"></param>
        /// <param name="Is16Bit">是否为16位字符串</param>
        /// <returns>密码字符串</returns>
        public static string ToMD5(this string s, bool Is16Bit)
        {
            string p = FormsAuthentication.HashPasswordForStoringInConfigFile(s, "MD5").ToLower();
            if (Is16Bit) p = p.Substring(8, 16);
            return p;
        }

        /// <summary>
        /// 转换成安全代码
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToSafeHtml(this string s)
        {
            s = Regex.Replace(s, @"<\s*(\/?)\s*(script|i?frame|style|html|body|title|link|meta|object|\?|\%)([^>]*?)>", string.Empty, RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @"(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)", "$1>", RegexOptions.IgnoreCase);
            s = Regex.Replace(s, @"<", "", RegexOptions.IgnoreCase);
            return s;
        }

        /// <summary>
        /// 转换成合法的文件名
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToFileName(this string s)
        {
            StringBuilder builder = new StringBuilder(s);
            builder.Replace("/", string.Empty);
            builder.Replace(@"\", string.Empty);
            builder.Replace(":", string.Empty);
            builder.Replace("*", string.Empty);
            builder.Replace("?", string.Empty);
            builder.Replace(",", string.Empty);
            builder.Replace("<", string.Empty);
            builder.Replace(">", string.Empty);
            builder.Replace("|", string.Empty);
            return builder.ToString();
        }

        public static string UrlDecode(this string s)
        {
            return HttpContext.Current.Server.HtmlDecode(s);
        }

        public static string UrlEncode(this string s)
        {
            return HttpContext.Current.Server.HtmlEncode(s);
        }

        public static string HtmlDecode(this string s)
        {
            return HttpContext.Current.Server.HtmlDecode(s);
        }

        public static string HtmlEncode(this string s)
        {
            s = HttpContext.Current.Server.HtmlEncode(s);
            s = s.Replace("\r\n", "<br />");
            s = s.Replace("\r", "<br />");
            s = s.Replace("\n", "<br />");
            return s;
        }

        /// <summary>
        /// 时间转换成多少小时前、多少分钟前、多少秒 
        /// </summary>
        /// <param name="dt">时间</param>
        /// <returns></returns>
        public static string DateStringFromNow(this DateTime dt)
        {
            TimeSpan span = DateTime.Now - dt;
            if (span.TotalDays > 60)
            {
                return dt.ToShortDateString();
            }
            else if (span.TotalDays > 30)
            {
                return "1个月前";
            }
            else if (span.TotalDays > 14)
            {
                return "2周前";
            }
            else if (span.TotalDays > 7)
            {
                return "1周前";
            }
            else if (span.TotalDays > 1)
            {
                return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
            }
            else if (span.TotalHours > 1)
            {
                return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
            }
            else if (span.TotalMinutes > 1)
            {
                return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
            }
            else if (span.TotalSeconds >= 1)
            {
                return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
            }
            else
            {
                return "刚刚";
            }
        }


        public static string WriterString(this string s, string str)
        {
            if (s.IsNullOrEmpty())
            {
                return str.ToSafeHtml();
            }
            return s.ToSafeHtml();
        }
    }
}
